package com.qc.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import com.qc.sys.dao.SysDeptDao;
import com.qc.sys.entity.SysDept;
import com.qc.sys.service.SysDeptService;
import com.qc.sys.vo.DeptVo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * (SysDept)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-08-29 14:02:25
 */
@Service
@EnableCaching
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDept> implements SysDeptService {

    @Autowired
    private SysDeptDao deptDao;

    @Override
    public DataGridView queryAllDept(DeptVo deptVo) {
        QueryWrapper<SysDept> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(deptVo.getTitle()),"title",deptVo.getTitle());
        qw.like(StringUtils.isNotBlank(deptVo.getAddress()),"title",deptVo.getAddress());
        List<SysDept> depts = this.deptDao.selectList(qw);
        return new DataGridView(Long.valueOf(depts.size()),depts);
    }

    @CachePut(cacheNames = "SysDeptServiceImpl",key = "#result.id")
    @Override
    public SysDept saveDept(SysDept dept) {
        this.deptDao.insert(dept);
        return dept;
    }

    @Override
    public Integer queryDeptMaxOrderNum() {
        return this.deptDao.getMaxOrderNum();
    }

    @CachePut(cacheNames = "SysDeptServiceImpl",key = "#result.id")
    @Override
    public SysDept updateDept(SysDept dept) {
        SysDept sysDept = this.deptDao.selectById(dept.getId());
        BeanUtil.copyProperties(dept,sysDept, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        this.deptDao.updateById(sysDept);
        return sysDept;
    }

    @Cacheable(cacheNames = "SysDeptServiceImpl",key = "#id")
    @Override
    public SysDept getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public Integer queryDeptChildrenCountById(Integer id) {
        return this.deptDao.queryDeptCountById(id);

    }

    @CacheEvict(cacheNames = "SysDeptServiceImpl",key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }
}